package com.webemail.sys.service.impl;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.webemail.email.entity.Email;
import com.webemail.email.mapper.EmailMapper;
import com.webemail.sys.mapper.SysEmailMapper;
import com.webemail.sys.model.SysEmailAccount;
import com.webemail.sys.service.EmailService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author LaiHQ
 */
@Service
public class EmailServiceImpl implements EmailService {
	private Logger logger = LoggerFactory.getLogger(getClass());

	@Resource
	public SysEmailMapper sysEmailMapper;

	@Override
	public int insert(SysEmailAccount temp) {
		// TODO Auto-generated method stub
		logger.info("class:" + this.getClass().getName() + " method:" + "insert" + " param:" + JSON.toJSONString(temp));
		return sysEmailMapper.insert(temp);
	}

	@Override
	public int delete(SysEmailAccount temp) {
		// TODO Auto-generated method stub
		logger.info("class:" + this.getClass().getName() + " method:" + "update" + " param:" + JSON.toJSONString(temp));
		SysEmailAccount sysEmailAccount = sysEmailMapper.selectOne(Wrappers.lambdaQuery(SysEmailAccount.class)
				.eq(SysEmailAccount::getId,temp.getId())
				.eq(SysEmailAccount::getUserId,temp.getUserId()));
		sysEmailAccount.setDeleted(true);
		return sysEmailMapper.updateById(sysEmailAccount);
	}

	@Override
	public int delete(long id, long userId) {
		// TODO Auto-generated method stub
		logger.info("class:" + this.getClass().getName() + " method:" + "delete" + " param:" + "id:" + id + "userId:"
				+ userId);
		SysEmailAccount temp = new SysEmailAccount();
		temp.setId(id);
		temp.setUserId(userId);
		return sysEmailMapper.deleteById(temp);
	}

	@Override
	public int update(SysEmailAccount temp) {
		// TODO Auto-generated method stub
		logger.info("class:" + this.getClass().getName() + " method:" + "update" + " param:" + JSON.toJSONString(temp));
		return sysEmailMapper.updateById(temp);
	}

	@Override
	public SysEmailAccount get(long id, long userId) {
		// TODO Auto-generated method stub
		logger.info("class:" + this.getClass().getName() + " method:" + "get" + " param:" + "id:" + id + "userId:"
				+ userId);
		return sysEmailMapper.selectOne(Wrappers.lambdaQuery(SysEmailAccount.class)
				.eq(SysEmailAccount::getId,id)
				.eq(SysEmailAccount::getUserId,userId));
	}

	@Override
	public List<SysEmailAccount> list(long userId) {
		// TODO Auto-generated method stub
		logger.info("class:" + this.getClass().getName() + " method:" + "list" + " param:" + "userId:" + userId);
		return sysEmailMapper.selectList(Wrappers.lambdaQuery(SysEmailAccount.class)
				.eq(SysEmailAccount::getDeleted,false)
				.eq(SysEmailAccount::getUserId,userId));
	}

}
